#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<uint64_t> pentagonal_numbers {1, 5};
    uint64_t n = 2;
    int j = n - 1;
    while (true) {
        for (auto i = j - 1; i >= 0; --i) {
            uint64_t d = pentagonal_numbers[j] - pentagonal_numbers[i];
            if (!binary_search(pentagonal_numbers.begin(), pentagonal_numbers.end(), d)) 
                continue;
            
            uint64_t s = pentagonal_numbers[j] + pentagonal_numbers[i];
            while (pentagonal_numbers.back() < s) {
                ++n;
                pentagonal_numbers.push_back(n * (3 * n - 1) / 2);
            }
            
            if (binary_search(pentagonal_numbers.begin(), pentagonal_numbers.end(), s)) {
                cout << d << endl;
                return 0;
            }
        }
        ++j;
        ++n;
        pentagonal_numbers.push_back(n * (3 * n - 1) / 2);
    }
}